package ws.very.util.lang
trait InputExitCommon {
  def isExit(msg: String): Boolean
  def run = {
    while (!isExit(Console.readLine)) {}
    beforeExit
    exitAction
  }
  def beforeExit = {
    println("exit")
  }
  def exitAction = System.exit(1)
}

@deprecated
trait InputExitableApp extends App with InputExitCommon {
  abstract override def delayedInit(body: => Unit) {
    println("be call")
    super.delayedInit {
      body
      run
    }
  }

}

trait InputExitApp extends App with ThreadConverter with InputExitCommon {
  abstract override def delayedInit(body: => Unit) {
    super.delayedInit {
      run.start
      body
    }
  }
}

trait AnyInputExitableApp extends InputExitCommon {
  def isExit(msg: String) = msg!=null && msg.length() > 4
}

trait Args2SysPropApp extends App {
  private lazy val SystemProperty = "-D([^=]+)=(.*)".r
  private def extractSystemProperties(javaOptions: Seq[String]): Seq[(String, String)] = {
    javaOptions.collect { case SystemProperty(key, value) => key -> value }
  }
  abstract override def delayedInit(body: => Unit) =
    super.delayedInit {
      args.collect { case SystemProperty(k, v) => k -> v }.foreach {
        case (k, v) =>
          System.setProperty(k, v)
      }
      body
    }
}